﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATMSystem
{
    class Bank
    {
        readonly List<Account> accountList = new List<Account>();
        static int accountCounter;

        public int CreateAccount()
        {
            Account account = new Account(++accountCounter);
            accountList.Add(account);
            return accountCounter;
        }

        public decimal CloseAccount(int id)
        {
            for (int i = 0; i < accountList.Count; i++)
            {
                if (accountList[i].Id == id)
                {
                    decimal balance = accountList[i].GiveMoney();
                    if (accountList.Remove(accountList[i]))
                    {
                        Console.WriteLine("Счет {0} успешно удалён", id);
                        return balance;
                    }
                }
            }
            Console.WriteLine("Счет {0} не может быть удалён", id);
            return 0;
        }

        public void GetAccountBalance(int id)
        {
            bool isAccountFound = false;
            for (int i = 0; i < accountList.Count; i++)
            {
                if (accountList[i].Id == id)
                {
                    Console.WriteLine("Баланс счета {0}: {1}", id, accountList[i].Balance);
                }
            }
            if (!isAccountFound)
            {
                Console.WriteLine("Счет {0} не найден", id);
            }
        }

        public bool GetMoney(int id, decimal money)
        {
            bool isAccountFound = false;
            for (int i = 0; i < accountList.Count; i++)
            {
                if (accountList[i].Id == id)
                {
                    isAccountFound = true;
                    accountList[i].GetMoney(money);
                    return true;
                }
            }
            if (!isAccountFound)
            {
                Console.WriteLine("Счет {0} не найден", id);
            }
            return false;
        }

        public decimal GiveMoney(int id, decimal money)
        {
            bool isAccountFound = false;
            for (int i = 0; i < accountList.Count; i++)
            {
                if (accountList[i].Id == id)
                {
                    isAccountFound = true;
                    if (accountList[i].GiveMoney(money))
                    {
                        return money;
                    }
                }
            }
            if (!isAccountFound)
            {
                Console.WriteLine("Счет {0} не найден", id);
            }
            return 0;
        }
    }
}
